package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.common.S;
import com.gaohe.pojo.Classes;
import com.gaohe.pojo.User;
import com.gaohe.service.ClassesService;
import com.gaohe.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/classes")
public class ClassesController {
    @Autowired
    private ClassesService classesService;
    @Autowired
    private UserService userService;

    /**
     * 分页
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        Page<Classes> classesPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.like(ObjectUtils.isNotEmpty(name), Classes::getName, name);
        qw.eq(Classes::getIsDeleted,S.NOT_DELETED);
        qw.orderByDesc(Classes::getUpdateTime);
        classesService.page(classesPage, qw);
        return R.success(classesPage);
    }
    /**
     * 回收站
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page1")
    public R page1(Integer page, Integer pageSize, String name) {
        Page<Classes> classesPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.like(ObjectUtils.isNotEmpty(name), Classes::getName, name);
        qw.eq(Classes::getIsDeleted,S.IS_DELETED);
        qw.orderByDesc(Classes::getUpdateTime);
        classesService.page(classesPage, qw);
        return R.success(classesPage);
    }

    /**
     * TODO 图片
     * 新增
     * @param classes
     * @return
     */
    @PostMapping
    public R insert(@RequestBody Classes classes) {
        String tname = classes.getTname();
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(ObjectUtils.isNotEmpty(classes.getTname()),User::getName,tname);
        User user = userService.getOne(qw);
        classes.setTeacherId(user.getId());
        return classesService.save(classes)? R.success(S.INSERT_SUCCESS) : R.error(S.INSERT_ERROR);
    }

    /**
     * 修改
     * @param classes
     * @return
     */
    @PutMapping
    public R update(@RequestBody Classes classes){
        //获取课程 id
        String tname = classes.getTname();
        Classes classesOne = classesService.getById(classes.getId());
        //判断有没有修改老师
        if (!classesOne.getTname().equals(tname)){
            LambdaQueryWrapper<User> userqw = new LambdaQueryWrapper<>();
            userqw.eq(User::getName,tname);
            userqw.select(User::getId);
            User user = userService.getOne(userqw);
            classes.setTeacherId(user.getId());
        }
        return classesService.updateById(classes) ? R.success(S.UPDATE_SUCCESS):R.error(S.UPDATE_ERROR);
    }

    /**
     * 根据 id 查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id){
        return R.success(classesService.getById(id));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public R deleteById(Long id){
        Classes classes = new Classes();
        classes.setId(id);
        classes.setIsDeleted(S.IS_DELETED);
        return classesService.updateById(classes)?R.success(S.DELETE_SUCCESS):R.error(S.DELETE_ERROR);
    }

    /**
     * 恢复
     * @param id
     * @return
     */
    @DeleteMapping("/restore")
    public R restoreById(Long id){
        Classes classes = new Classes();
        classes.setId(id);
        classes.setIsDeleted(S.NOT_DELETED);
        return classesService.updateById(classes)?R.success(S.DELETE_SUCCESS):R.error(S.DELETE_ERROR);
    }

    /**
     * 根据 课程 id 查询 用户
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    public R getUserByClassesId(@PathVariable Long id){
        List<User> userList = classesService.getUserByClassesId(id);
        return R.success(userList);
    }

    @GetMapping("/getClassByCardTags")
    public R getClassesByCardTags(@RequestParam String tags){
        //select * from classes where tags like ...
        List<String> classesList = classesService.getClassesByCardTags(tags);
        return R.success(classesList);
    }

    /**
     * 用户查看课程
     * @param tags
     * @return
     */
    @GetMapping("/getClassList/{tags}")
    public R getClassList(@PathVariable String tags){
        List<Classes> classesList = classesService.getClassList(tags);
        return R.success(classesList);
    }
}
